package com.sg.service.biz.app.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.biz.app.common.DataFilterSpecRecBy;
import com.sg.dto.biz.app.req.*;
import com.sg.dto.biz.app.res.*;
import com.wicket.okrapp.common.baseinfo.BaseInfoDO;
import com.wicket.okrapp.common.baseinfo.BaseInfoHolder;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbTargetContent {
    /**
     * code:getCurrentContentCode
     * name:M3输出当前内容标识（特殊方法）
     * desc:undefined
     * gen by moon at 1/1/2023, 9:14:03 PM
     **/
    @Trace(operationName = "M3输出当前内容标识（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainObjectCodeComRespDto obtainObjectCodeCom(ObtainObjectCodeComReqDto reqDto) {
        ObtainObjectCodeComRespDto retData = new ObtainObjectCodeComRespDto();
        if (reqDto.getPeerContentCodeList() == null || reqDto.getPeerContentCodeList().size() == 0) {
            if (reqDto.getSuperiorContentCode().equals("1")) {
                retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "001");
            } else {
                retData.setCurrentContentCode(reqDto.getSuperiorContentCode() + "1001");
            }

        } else if (reqDto.getPeerContentCodeList().size() > 0) {
            String first = reqDto.getPeerContentCodeList().get(0);
            String preFix = first.substring(0, first.length() - 4);
            List<String> shortCodeList = reqDto.getPeerContentCodeList().stream().map(code -> code.substring(code.length() - 4)).collect(Collectors.toList());
            List<Long> codeNumList = shortCodeList.stream().map(code -> Long.valueOf(code)).collect(Collectors.toList());
            Long max = Collections.max(codeNumList) + 1;
            retData.setCurrentContentCode(preFix + String.valueOf(max));
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M3执行业务应用构造出入参字段（特殊方法）
     * desc:undefined
     * gen by moon at 1/1/2023, 9:16:16 PM
     **/
    @Trace(operationName = "M3执行业务应用构造出入参字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizConstructAccessParameterFieldRespDto implementBizConstructAccessParameterField(ImplementBizConstructAccessParameterFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementBizConstructAccessParameterFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3获取接收外部字段(特殊方法）
     * desc:undefined
     * gen by moon at 1/10/2023, 3:33:38 AM
     **/
    @Trace(operationName = "M3获取接收外部字段(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveOutFieldRespDto obtainReceiveOutField(ObtainReceiveOutFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveOutFieldRespDto.class);
    }

    @Trace(operationName = "M3获取接收外部字段(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveOutFieldByCoderRespDto obtainReceiveOutFieldByCoder(ObtainReceiveOutFieldByCoderReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveOutFieldByCoderRespDto.class);
    }

    /**
     * code:publicFieldCache
     * name:M3执行业务应用公共字段推送内存（特殊方法）
     * desc:undefined
     * gen by moon at 1/10/2023, 5:13:06 PM
     **/
    @Trace(operationName = "M3执行业务应用公共字段推送内存（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizPublicFieldPushMemoryRespDto implementBizPublicFieldPushMemory(ImplementBizPublicFieldPushMemoryReqDto reqDto) {
        BaseInfoDO domain = new BaseInfoDO();
        if (BaseInfoHolder.contextHolder.get() != null) {
            BeanUtil.copyProperties(BaseInfoHolder.contextHolder.get().getBaseInfo(), domain);
            BaseInfoHolder.contextHolder.remove();
        }
        if (reqDto.getSpaceId() != null) {
            domain.setSpecSpaceId(reqDto.getSpaceId());
        }

        if (reqDto.getCreateInductionId() != null) {
            domain.setSpecInductionRecordId(reqDto.getCreateInductionId());
        }

        if (reqDto.getAppId() != null) {
            domain.setSpecAppId(reqDto.getAppId());
        }

        if (reqDto.getHighestOrgID() != null) {
            domain.setSpecHighestOrgID(reqDto.getHighestOrgID());
        }

        if (reqDto.getUserId() != null) {
            domain.setSpecUserId(reqDto.getUserId());
        }

        if (reqDto.getOriginalRoleMemberId() != null) {
            domain.setSpecOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());
        }

        CommonFunctionHelper.setBaseInfoToLocal(domain);
        return new ImplementBizPublicFieldPushMemoryRespDto();
    }

    /**
     * code:receptionService
     * name:M3执行获取出入参业务字段（特殊方法）
     * desc:undefined
     * gen by moon at 1/27/2023, 6:53:20 PM
     **/
    @Trace(operationName = "M3执行获取出入参业务字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAccessParameterBizFieldRespDto implementAccessParameterBizField(ImplementAccessParameterBizFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAccessParameterBizFieldRespDto.class);
    }

    /**
     * code:dataFilterSpecRecBy
     * name:M3-执行数据集按入参过滤指定记录（特殊方法）
     * desc:undefined
     * gen by moon at 2/4/2023, 1:08:39 AM
     **/
    @Trace(operationName = "M3-执行数据集按入参过滤指定记录（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementDataFilterSpecRecByRespDto implementDataFilterSpecRecBy(ImplementDataFilterSpecRecByReqDto reqDto) {
        ImplementDataFilterSpecRecByRespDto retData = new ImplementDataFilterSpecRecByRespDto();
        for (DataFilterSpecRecBy one : reqDto.getDataFilterSpecRecByList()) {
            if (!one.getCommPrimaryKey().equals(reqDto.getCommPrimaryKey())) {
                retData.getDataFilterSpecRecByList().add(one);
            }
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M3-获取接收字段（特殊方法）
     * desc:undefined
     * gen by moon at 2/4/2023, 1:11:23 AM
     **/
    @Trace(operationName = "M3-获取接收字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveFieldRespDto obtainReceiveField(ObtainReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3拼合过程执行周期阶段与内容目标子周期字段
     * desc:undefined
     * gen by moon at 2/10/2023, 9:55:18 PM
     **/
    @Trace(operationName = "M3拼合过程执行周期阶段与内容目标子周期字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptAboveFieldsRespDto implementAcceptAboveFields(ImplementAcceptAboveFieldsReqDto reqDto) {
        ImplementAcceptAboveFieldsRespDto retData = new ImplementAcceptAboveFieldsRespDto();
        retData.setProcessExceuteCycleStageList(reqDto.getProcessExceuteCycleStageList());
        return retData;
    }

    /**
     * code:receptionService
     * name:M3接收指标列表
     * desc:undefined
     * gen by moon at 2/11/2023, 9:13:54 PM
     **/
    @Trace(operationName = "M3接收指标列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptParameterBizFieldRespDto implementAcceptParameterBizField(ImplementAcceptParameterBizFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptParameterBizFieldRespDto.class);
    }

    /**
     * code:findAllLevelCodeList
     * name:M-查找祖先
     * desc:undefined
     * gen by moon at 3/17/2023, 10:00:34 PM
     **/
    @Trace(operationName = "M-查找祖先")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryLookAncestorNodeListRespDto queryLookAncestorNodeList(QueryLookAncestorNodeListReqDto reqDto) {
        QueryLookAncestorNodeListRespDto retData = new QueryLookAncestorNodeListRespDto();
        String oriCode = null;
        if (reqDto.getEvaObjTargetCycleCode() != null && !reqDto.getEvaObjTargetCycleCode().isEmpty()) {
            oriCode = reqDto.getEvaObjTargetCycleCode();
        }

        if (reqDto.getAssistNodeTypeObjectCode() != null && !reqDto.getAssistNodeTypeObjectCode().isEmpty()) {
            oriCode = reqDto.getAssistNodeTypeObjectCode();
        }

        if (reqDto.getComCode() != null && !reqDto.getComCode().isEmpty()) {
            oriCode = reqDto.getComCode();
        }

        if (oriCode == null || oriCode.isEmpty()) {
            return retData;
        }
        int dealSize = 4;
        while (oriCode.length() >= dealSize) {
            String oneCode = oriCode.substring(0, dealSize);
            if (oneCode != null && !oneCode.isEmpty()) {
                EvaObjTargetCycleDto elm = new EvaObjTargetCycleDto();
                elm.setEvaObjTargetCycleCode(oneCode);
                retData.getEvaObjTargetCycleList().add(elm);
                retData.getLookAncestorNodeList().add(oneCode);
            }
            dealSize = dealSize + 4;
        }
        retData.getLookAncestorNodeList().remove(oriCode);
        return retData;

    }

    /**
     * code:receptionService
     * name:M3接收上文出参字段
     * desc:undefined
     * gen by moon at 3/22/2023, 11:12:21 PM
     **/
    @Trace(operationName = "M3接收上文出参字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldRespDto implementAcceptField(ImplementAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3约定为已完成
     * desc:undefined
     * gen by moon at 10/7/2023, 11:43:49 AM
     **/
    @Trace(operationName = "M3约定为已完成")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementGainInParameterFieldsRespDto implementGainInParameterFields(ImplementGainInParameterFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementGainInParameterFieldsRespDto.class);

    }

    /**
     * code:receptionService
     * name:约定：否
     * desc:undefined
     * gen by moon at 2/6/2024, 4:04:47 AM
     **/
    @Trace(operationName = "约定：否")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFourthReceivingFieldRespDto implementFourthReceivingField(ImplementFourthReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementFourthReceivingFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3约定是与否
     * desc:undefined
     * gen by moon at 2/26/2024, 11:38:35 PM
     **/
    @Trace(operationName = "M3约定是与否")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFiveReceivingFieldRespDto implementFiveReceivingField(ImplementFiveReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementFiveReceivingFieldRespDto.class);

    }

    /**
     * code:getSpecRecInSortedDataList
     * name:M3获取已排序数据集中的指定周期（分析出结束子周期）
     * desc:undefined
     * gen by moon at 3/26/2024, 1:28:13 AM
     **/
    @Trace(operationName = "M3获取已排序数据集中的指定周期（分析出结束子周期）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainGainSortDataSetAssignCycleRespDto obtainGainSortDataSetAssignCycle(ObtainGainSortDataSetAssignCycleReqDto reqDto) {
        // TODO ruizhe skai dong ; 3/26/2024, 1:28:13 AM
        return new ObtainGainSortDataSetAssignCycleRespDto();
    }

    /**
     * code:receptionService
     * name:M3接收是否实际验收周期
     * desc:undefined
     * gen by moon at 8/19/2024, 9:47:12 PM
     **/
    @Trace(operationName = "M3接收是否实际验收周期")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementElevenReceivingFieldRespDto implementElevenReceivingField(ImplementElevenReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementElevenReceivingFieldRespDto.class);
    }
    //手工接入方法
}
